package com.walkline.dianping.dao;

import java.util.Hashtable;

import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;

import com.walkline.dianping.DianpingException;
import com.walkline.dianping.DianpingSDK;
import com.walkline.dianping.inf.Categories;
import com.walkline.util.Function;

public class DianpingCategories extends DianpingObject implements Categories
{
	private Hashtable _subcategories = new Hashtable();
	private String[] _categoriesName;

	public DianpingCategories(DianpingSDK dianping, JSONObject jsonObject) throws DianpingException
	{
		super(dianping, jsonObject);

		JSONArray categoriesArray = _jsonObject.optJSONArray("categories");
		JSONObject categories = new JSONObject();

		try {
			if (categoriesArray != null)
			{
				_categoriesName = new String[categoriesArray.length()];

				for (int i=0; i<categoriesArray.length(); i++)
				{
					JSONObject category = categoriesArray.getJSONObject(i);

					if (category != null)
					{
						String categoryName = category.optString("category_name");
						_categoriesName[i] = categoryName;

						JSONArray subCategoriesArray = category.optJSONArray("subcategories");
						if (subCategoriesArray != null)
						{
							String[] subCategoriesName = new String[subCategoriesArray.length()];

							for (int j=0; j<subCategoriesArray.length(); j++)
							{
								JSONObject subCategory = subCategoriesArray.getJSONObject(j);

								if (subCategory != null)
								{
									subCategoriesName[j] = subCategory.optString("category_name");
								}
							}

							_subcategories.put(categoryName, subCategoriesName);
						}
					}
				}
			}
		} catch (JSONException e) {Function.errorDialog(e.toString());}

		if (categories != null)
		{

			/*
			JSONArray _districtsArray = cities.optJSONArray("districts");
			if (_districtsArray != null)
			{
				_districts = new String[_districtsArray.length()];

				for (int i=0; i<_districtsArray.length(); i++)
				{
					JSONObject district;
					try {
						district = _districtsArray.getJSONObject(i);

						if (district != null)
						{
							String districtName = district.optString("district_name");
							_districts[i] = districtName;

							JSONArray jsonArray = district.optJSONArray("neighborhoods");
							String[] neighborhoodsArray = new String[jsonArray.length()];

							for (int j=0; j<jsonArray.length(); j++)
							{
								try {
									neighborhoodsArray[j] = jsonArray.get(j).toString();
								} catch (JSONException e) {}
							}

							_neighborhoods.put(districtName, neighborhoodsArray);
						}
					} catch (JSONException e) {Function.errorDialog(e.toString());}
				}
			}
			*/
		}
	}

	/**
	 * 本次API访问状态，如果成功返回"OK"，并返回结果字段，如果失败返回"ERROR"，并返回错误说明
	 */
	public String getStatus() {return _jsonObject.optString("status");}

	public String[] getCategoriesName() {return _categoriesName;}

	public String[] getSubCategoriesName(Object key) {return (String[]) _subcategories.get(key);}

	public String[] getSubCategoriesName(com.walkline.dianping.inf.Object key) {
		// TODO Auto-generated method stub
		return null;
	}
}